home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
SGI Developer Toolbox 6.1
/
SGI Developer Toolbox 6.1 - Disc 4.iso
/
src
/
haeberli
/
impression
/
ssrand.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-08-01
|
4KB
|
211 lines
/*
* Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
* All Rights Reserved.
*
* This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
* the contents of this file may not be disclosed to third parties, copied or
* duplicated in any form, in whole or in part, without the prior written
* permission of Silicon Graphics, Inc.
*
* RESTRICTED RIGHTS LEGEND:
* Use, duplication or disclosure by the Government is subject to restrictions
* as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
* and Computer Software clause at DFARS 252.227-7013, and/or in similar or
* successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
* rights reserved under the Copyright Laws of the United States.
*/
/*
* ssrand -
* Add noise to a sample set
*
* Paul Haeberli - 1988
*/
#include "stdio.h"
#include "ss.h"
#include "math.h"
#define COLOR 1
#define DIRECTION 2
#define POSITION 4
#define SIZE 8
main(argc,argv)
int argc;
char **argv;
{
sampleset *iss, *oss;
float mag;
int i, mode;
if( argc<3 ) {
fprintf(stderr,"usage: ssrand in.ss out.ss mag [-c -d -p -s]\n");
exit(1);
}
srand(getpid());
mode = 0;
if(argc>3) {
mag = atof(argv[3]);
for(i=4; i<argc; i++) {
if(strcmp(argv[i],"-c") == 0)
mode |= COLOR;
if(strcmp(argv[i],"-d") == 0)
mode |= DIRECTION;
if(strcmp(argv[i],"-p") == 0)
mode |= POSITION;
if(strcmp(argv[i],"-s") == 0)
mode |= SIZE;
}
}
iss = ssfromfile(argv[1]);
oss = ssnew();
if(mode == 0)
ssrandord(iss,oss);
else
dossrand(iss,oss,mode,mag);
sstofile(argv[2],oss);
}
ssrandord(iss,oss)
sampleset *iss, *oss;
{
sample *tab[256];
sample *s, *ns;
int i;
for(i=0; i<256; i++)
tab[i] = 0;
s = iss->head;
while(s) {
ns = sclone(s);
i = rand()%256;
ns->next = tab[i];
tab[i] = ns;
s = s->next;
}
for(i=0; i<256; i++) {
while(tab[i]) {
ns = tab[i];
tab[i] = ns->next;
addsample(oss,ns);
}
}
}
dossrand(iss,oss,mode,mag)
sampleset *iss, *oss;
int mode;
float mag;
{
sample *s, *ns;
int imag, bimag;
imag = 255*mag;
bimag = 32000*mag;
s = iss->head;
while(s) {
ns = sclone(s);
if(mode & COLOR) {
modifycolor(ns,imag);
}
if(mode & DIRECTION) {
ns->at1 += mynoise(imag);
}
if(mode & POSITION) {
ns->xpos += mynoise(bimag);
ns->ypos += mynoise(bimag);
}
if(mode & SIZE) {
ns->at0 = (ns->at0*(255+mynoise(imag)))/255;
}
addsample(oss,ns);
s = s->next;
}
}
modifycolor(ns,imag)
sample *ns;
int imag;
{
float fr, fg, fb;
float h, s, v;
int ih, is, iv;
#define MOD_RGB
#ifdef MOD_RGB
ns->r = addnoise(ns->r,imag);
ns->g = addnoise(ns->g,imag);
ns->b = addnoise(ns->b,imag);
#endif
#ifdef MOD_HUE
fr = ns->r/255.0;
fg = ns->g/255.0;
fb = ns->b/255.0;
rgb_to_hsv(fr,fg,fb,&h,&s,&v);
ih = 255*h;
ih = ih + mynoise(imag);
if(ih<0)
ih += 256;
if(ih>255)
ih -= 256;
if(ih<0)
ih = 0;
if(ih>255)
ih = 255;
h = ih/255.0;
hsv_to_rgb(h,s,v,&fr,&fg,&fb);
ns->r = 255*fr;
ns->g = 255*fg;
ns->b = 255*fb;
#endif
#ifdef MOD_VALUE
fr = ns->r/255.0;
fg = ns->g/255.0;
fb = ns->b/255.0;
rgb_to_hsv(fr,fg,fb,&h,&s,&v);
iv = 255*v;
iv = addnoise(iv,imag);
v = iv/255.0;
hsv_to_rgb(h,s,v,&fr,&fg,&fb);
ns->r = 255*fr;
ns->g = 255*fg;
ns->b = 255*fb;
#endif
#ifdef MOD_SAT
fr = ns->r/255.0;
fg = ns->g/255.0;
fb = ns->b/255.0;
rgb_to_hsv(fr,fg,fb,&h,&s,&v);
is = 255*s;
is = addnoise(is,imag);
s = is/255.0;
hsv_to_rgb(h,s,v,&fr,&fg,&fb);
ns->r = 255*fr;
ns->g = 255*fg;
ns->b = 255*fb;
#endif
}
mynoise(imag)
int imag;
{
if(imag<2)
imag = 2;
return (rand()%(imag<<1))-imag;
}
addnoise(v,amp)
int v, amp;
{
if(v<amp)
amp = v;
if((255-v)<amp)
amp = 255-v;
if(amp)
return v+(rand()%(amp<<1))-amp;
else
return v;
}